﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class frm供货商信息 : Form
    {
        public frm供货商信息()
        {
            InitializeComponent();
        }
        string str = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
       //加载窗体
        private void frm供货商信息_Load(object sender, EventArgs e)
        {
            chaxun();
        }
        //查询按钮
        private void btnchaxun_Click(object sender, EventArgs e)
        {
            chaxun();
        }
        //查询代码
        public void chaxun() {
            SqlConnection conn = new SqlConnection(str);
            string sql = "select id,idquan,name,appone,phone,beizhu from dbo.jinguocaigou where id like'%"+this.txtname.Text+"%'";
            DataSet ds = new DataSet("Student");
            SqlDataAdapter asdf = new SqlDataAdapter(sql,conn);
            asdf.Fill(ds,"Student");
           this.dgvghs.DataSource= ds.Tables["Student"] ;

        }
        //新增
        private void btnzngjia_Click(object sender, EventArgs e)
        {
            frm供货商新增 frm = new frm供货商新增();
            frm.ShowDialog();
            chaxun();
        }
        //修改
        private void btnxiugai_Click(object sender, EventArgs e)
        {
            if (this.dgvghs.SelectedRows.Count > 0)
            {
                frm供货商修改 frm = new frm供货商修改();
                frm.id = this.dgvghs.SelectedCells[0].Value.ToString();
                frm.ShowDialog();
                chaxun();
            }
            else
            {
                MessageBox.Show("选择一行", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
           
        }
      //删除
        private void btnshanchu_Click(object sender, EventArgs e)
        {
            if (this.dgvghs.SelectedRows.Count > 0)
            {
                DialogResult dsa = MessageBox.Show("确定删除吗","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
               if(dsa==DialogResult.OK){
                string id = this.dgvghs.SelectedCells[0].Value.ToString() ;
                SqlConnection conn = new SqlConnection(str);
                conn.Open();
                try
                {
                   
                    string sql = "delete dbo.jinguocaigou where id='" + id + "'";
                    SqlCommand comm = new SqlCommand(sql, conn);
                    int i = comm.ExecuteNonQuery();
                    if (i > 0)
                    {
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        chaxun();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally {
                    conn.Close();
                }
            }
            }
            else {
                MessageBox.Show("选择一行","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
            }

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
//退出
        private void btndaochu_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtname_TextChanged(object sender, EventArgs e)
        {
            chaxun();
        }

    }
}
